// pages/exclusive-shop/index.js
const app = getApp()
const {
    globalData
} = app

Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList: [], // 轮播图列表
        menuList: [], // 菜单列表
        productTypeList: [], // 商品分类列表
        adList: [], // 商品分类下的广告列表
    },

    menuClickHandle(event) {
        const {row, col} = event.currentTarget.dataset
        console.log(row, col)
        this.requestTypeProductList(this.data.menuList[row][col].id)
    },

    // 请求banner
    requestBannerList() {
        wx.ajax.post('queryMerchantBanner.action', {
            merchant_id: 'plantform'
        }).then(res => {
            let bannerList = res.result.banner_img || []
            bannerList.forEach(item => {
                item.head_img = globalData.imageUrl + item.head_img
            })

            this.setData({
                refreshed: false,
                showMoreBtn: !!res.result.showMoreBtn,
                bannerList: bannerList
            })
        })
    },

    // 请求菜单
    requestMenuList() {
        wx.ajax.post('queryMonopolyMenuList.action', {}, true).then(res => {
            let productMenut;
            let list = (res.result || []).map(item => {
                if (item.type == '1') {
                    productMenut = item
                }

                return {
                    ...item,
                    image_path: globalData.imageUrl + item.image_path,
                }
            })

            let menuList = []
            while(list.length > 0) {
                menuList.push(list.splice(0, 5))
            }

            this.setData({
                menuList
            }, () => {
                if (productMenut) {
                    this.requestAdListAndTypeProductList(productMenut.id)
                } else {
                    this.requestTypeBannerList()
                }
            })
        })
    },
    // 请求广告和商品
    requestAdListAndTypeProductList(monopolyMenuId) {
        Promise.all([
            wx.ajax.post("queryBannerList.action?type=4", {}, true),
            wx.ajax.post('queryMonopolyMenuTypeList.action', {
                monopolyMenuId
            }, false)
        ]).then(([adRes, productRes]) => {
            let productTypeList = []

            if (productRes.result) {
                productRes.result.forEach((item) => {
                    if (item.product_info_vos && item.product_info_vos.length > 0) {
                        productTypeList.push({
                            ...item,
                            typeName: item.product_name,
                            typeId: item.product_type,
                            productList: item.product_info_vos.map((product) => {
                                return {
                                    ...product,
                                    head_img: globalData.imageUrl + product.head_img,
                                };
                            }),
                        });
                    }
                });
            }

            this.setData({
                adList: (adRes.result || []).map((item) => {
                    return {
                        ...item,
                        image_path: globalData.imageUrl + item.image_path,
                    };
                }),
                productTypeList
            })
        })
    },

    // 请求分类banner
    requestTypeBannerList() {
        wx.ajax.post("queryBannerList.action?type=4", {}, false).then(res => {
            this.setData({
                adList: (res.result || []).map((item) => {
                    return {
                        ...item,
                        image_path: globalData.imageUrl + item.image_path,
                    };
                }),
            })
        })
    },
    // 请求分类商品
    requestTypeProductList(monopolyMenuId) {
        wx.ajax.post('queryMonopolyMenuTypeList.action', {
            monopolyMenuId
        }, true).then(res => {
            let productTypeList = []

            if (res.result) {
                res.result.forEach((item) => {
                    if (item.product_info_vos && item.product_info_vos.length > 0) {
                        productTypeList.push({
                            ...item,
                            typeName: item.product_name,
                            typeId: item.product_type,
                            productList: item.product_info_vos.map((product) => {
                                return {
                                    ...product,
                                    head_img: globalData.imageUrl + product.head_img,
                                };
                            }),
                        });
                    }
                });
            }

            this.setData({
                productTypeList
            })
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.requestBannerList()
        this.requestMenuList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})